//
//  MyTradingRecordVC.m
//  MRBang
//
//  Created by 铅笔 on 16/4/20.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "MyTradingRecordVC.h"
#import "RechargeVC.h"//充值
#import "WithdrawalsVC.h"//提现

#import "MyTradingRecordCell.h"

#define CELLHEIGHT 80*PROPORTION_WIDTH

@interface MyTradingRecordVC ()
//<UITableViewDataSource,UITableViewDelegate>
{
    UIView *view_topBackground;//头部信息背景
    
    NSString *str_couldUseMoney;//可用金额

}

//@property (nonatomic,strong) UITableView *tableView_detail;

@end

@implementation MyTradingRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的交易记录";
    
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    
    [self initWithDetailTableViewSubviews];
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    //网络请求
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self DataRequestUserInfo];
    
    
}


#pragma mark --- 头部信息

- (void) initWithTopViewSubviews
{
    view_topBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 244*PROPORTION_WIDTH)];
    [self.view addSubview:view_topBackground];
    view_topBackground.backgroundColor = [UIColor whiteColor];
    
    //line
    UIView *view_line = [[UIView alloc] initWithFrame:CGRectMake(0,view_topBackground.height/2, kScreenWidth, 1.0)];
    [view_topBackground addSubview:view_line];
    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#eeeff1"];
    
    NSArray *array_title = @[@"可用金额",@"在途金额"];
    
    for (int i=0; i<2; i++) {
        UILabel *label_leftTitle = [[UILabel alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, 48*PROPORTION_WIDTH+120*PROPORTION_WIDTH*i, 110*PROPORTION_WIDTH, 30*PROPORTION_WIDTH)];
        [view_topBackground addSubview:label_leftTitle];
        label_leftTitle.text = array_title[i];
        label_leftTitle.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
        label_leftTitle.textColor = [BaseViewController colorWithHexString:@"#666666"];
        
        UILabel *label_rightTitle = [[UILabel alloc] initWithFrame:CGRectMake(30*PROPORTION_WIDTH+label_leftTitle.right, 48*PROPORTION_WIDTH+120*PROPORTION_WIDTH*i, 440*PROPORTION_WIDTH, 30*PROPORTION_WIDTH)];
        [view_topBackground addSubview:label_rightTitle];
        label_rightTitle.text = @"0.00元";
        label_rightTitle.font = [UIFont systemFontOfSize:30*PROPORTION_WIDTH];
        label_rightTitle.textColor = [BaseViewController colorWithHexString:@"#e8482e"];
        if (i==0) {
           label_rightTitle.text = str_couldUseMoney;
        }
    }
    //label
    UILabel *label_detial = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth-116*PROPORTION_WIDTH, 48*PROPORTION_WIDTH+120*PROPORTION_WIDTH, 50*PROPORTION_WIDTH, 30*PROPORTION_WIDTH)];
    [view_topBackground addSubview:label_detial];
    label_detial.text = @"明细";
    label_detial.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
    label_detial.textColor = [BaseViewController colorWithHexString:@"#666666"];
    
    //button
    UIButton *button_rightArrow = [[UIButton alloc] initWithFrame:CGRectMake(label_detial.right, label_detial.y-2.2, 20, 20)];
    [view_topBackground addSubview:button_rightArrow];
    [button_rightArrow setImage:[UIImage imageNamed:@"return_right"] forState:UIControlStateNormal];
    //button
    UIButton *button_helpMeGet = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-116*PROPORTION_WIDTH, 41*PROPORTION_WIDTH, 85*PROPORTION_WIDTH, 42*PROPORTION_WIDTH)];
    [view_topBackground addSubview:button_helpMeGet];
//    [button_helpMeGet setImage:[UIImage imageNamed:@"button_get"] forState:UIControlStateNormal];
    button_helpMeGet.backgroundColor = COLOR_BUTTON;
    [button_helpMeGet setTitle:@"帮我理" forState:UIControlStateNormal];
    [button_helpMeGet setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button_helpMeGet.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_18]];
    button_helpMeGet.layer.cornerRadius = 3.0;
    button_helpMeGet.clipsToBounds = YES;
}

#pragma mark --- 创建明细表
- (void) initWithDetailTableViewSubviews
{
#warning 因为没有数据暂时 隐掉
//    self.tableView_detail = [[UITableView alloc] initWithFrame:CGRectMake(0, view_topBackground.bottom, kScreenWidth,CELLHEIGHT*5) style:UITableViewStyleGrouped];
//    [self.view addSubview:self.tableView_detail];
//    self.tableView_detail.dataSource = self;
//    self.tableView_detail.delegate = self;
//    self.tableView_detail.showsVerticalScrollIndicator = NO;
//    self.tableView_detail.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    CGFloat buttonW = (kScreenWidth - (33*2+24)*PROPORTION_WIDTH)/2;
    //button  recharge
    UIButton *button_recharge = [[UIButton alloc] initWithFrame:CGRectMake(33*PROPORTION_WIDTH, kScreenHeight-95*PROPORTION_WIDTH-10, buttonW, 95*PROPORTION_WIDTH)];
    [self.view addSubview:button_recharge];
    [button_recharge setImage:[UIImage imageNamed:@"button_drawMoney"] forState:UIControlStateNormal];
    button_recharge.tag = 21;
    [button_recharge addTarget:self action:@selector(rechargeAndWithdrawsAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //button  withdraw
    UIButton *button_withdraw = [[UIButton alloc] initWithFrame:CGRectMake(button_recharge.right+24*PROPORTION_WIDTH,button_recharge.y, buttonW, button_recharge.height)];
    [self.view addSubview:button_withdraw];
    [button_withdraw setImage:[UIImage imageNamed:@"button_rechangeMoney"] forState:UIControlStateNormal];
    button_withdraw.tag = 22;
    [button_withdraw addTarget:self action:@selector(rechargeAndWithdrawsAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void) rechargeAndWithdrawsAction:(UIButton *)sender
{
    if (sender.tag==21) {
        //提现
        WithdrawalsVC *withdrawView = [[WithdrawalsVC alloc] init];
        withdrawView.str_balance = str_couldUseMoney;
        [self.navigationController pushViewController:withdrawView animated:NO];
        
    }
    if (sender.tag==22) {
        //充值
//        RechargeVC *rechargeView = [[RechargeVC alloc] init];
//        [self.navigationController pushViewController:rechargeView animated:NO];
       
    }

}

//#pragma mark --- UITableViewDataSource
//- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 5;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *cellMyTradingRecordID = @"cellMyTradingRecordID";
//    MyTradingRecordCell *cell_myTradingRecord = [tableView dequeueReusableCellWithIdentifier:cellMyTradingRecordID];
//    if (cell_myTradingRecord == nil) {
//        cell_myTradingRecord = [[MyTradingRecordCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellMyTradingRecordID];
//    }
//    
//    UIView *view_line = [[UIView alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, CELLHEIGHT-1, kScreenWidth, 1.0)];
//    [cell_myTradingRecord.contentView addSubview:view_line];
//    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#EEEFF1"];
//    
//    cell_myTradingRecord.selectionStyle = UITableViewCellSelectionStyleNone;
//    
//    return cell_myTradingRecord;
//
//}
//
//- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view_top = [[UIView alloc] init];
//    
//    //label
//    UILabel *label_detial = [[UILabel alloc] initWithFrame:CGRectMake(28*PROPORTION_WIDTH, 10*PROPORTION_WIDTH, 150*PROPORTION_WIDTH, 35*PROPORTION_WIDTH)];
//    [view_topBackground addSubview:label_detial];
//    label_detial.text = @"交易明细";
//    label_detial.font = [UIFont systemFontOfSize:24*PROPORTION_WIDTH];
//    label_detial.textColor = [BaseViewController colorWithHexString:@"#666666"];
//    
//    [view_top addSubview:label_detial];
//    
//    view_top.backgroundColor = COLOR_EVERYVIEWBG;
//    
//    return view_top;
//}
//
//- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 80*PROPORTION_WIDTH;
//}
//
//- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 50*PROPORTION_WIDTH;
//
//}

#pragma mark --- 用户信息查询
//用于 修改 可用金额
- (void) DataRequestUserInfo
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        NSString *strUrl = [NSString stringWithFormat:@"%@member/info",MRAPI];
        NSDictionary *paras_info = @{@"memberid":[[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"]};
        
        [[HttpRequest shardWebUtil] getNetworkRequestURLString:strUrl parameters:paras_info success:^(id obj) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0) {
                
                if (![obj[@"balance"] isKindOfClass:[NSNull class]]) {
                    if (obj[@"balance"] == nil) {
                         str_couldUseMoney = [NSString stringWithFormat:@"0.00元"];
                    }
                    else
                    {
                      str_couldUseMoney = [NSString stringWithFormat:@"%@元",obj[@"balance"]];
                    }
                    
                }
                else
                {
                      str_couldUseMoney = [NSString stringWithFormat:@"0.00元"];
                }
                
                 [self initWithTopViewSubviews];
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            [BaseViewController showMessage:@"请求超时"];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
